// pages/doctor-my-rz/doctor-my-rz.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorId: "", //医生ID
    zhengshu: [],
    myHeadImg: '', //本地的头像url
    userInfo: {}, //获取微信的 头像，昵称，性别
    honorImg: '', //图片路径
    honorDesc: '', //荣誉证书说明
    honorId: '', //医生荣誉证书ID
    hasUserInfo: false,
    file: '', //文件
    tupian: "",
    imageone: "",
    imagetwo: "",
    credentialone: "荣誉证书",
    credentialtwo: "荣誉证书",
    imgUrl: app.globalData.imgUrl, //请求接口图片路径
  },
  // 上传荣誉证书1图片
  updateone: function(e) {
    
    var that = this;
    var credential = that.data.credentialone;
    var imgId = e; 
    console.log("aaaaaaaa    ", e.currentTarget.dataset.imgid);
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        console.log('ceshi', a);
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('上传荣誉证书1结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imageone: path
            });
            
            wx.request({
              url: app.globalData.baseUrl + 'doctor/doctor/saveHonor',
              data: {
                'honorImg': path,
                'honorDesc': credential
              },
              header: {
                'sign': app.globalData.sign,
                'token': app.globalData.token,
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
              },
              method: 'POST',
              success(res) {
                console.log('保存证书1:', res);
                that.del(imgId);
                that.onLoad();
              }
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 上传荣誉证书2图片
  updatetwo: function() {
    var that = this;
    var credential = that.data.credentialtwo;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        console.log('ceshi', a);
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('上传荣誉证书2结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imagetwo: path
            })
            wx.request({
              url: app.globalData.baseUrl + 'doctor/doctor/saveHonor',
              data: {
                'honorImg': path,
                'honorDesc': credential
              },
              header: {
                'sign': app.globalData.sign,
                'token': app.globalData.token,
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
              },
              method: 'POST',
              success(res) {
                console.log('保存证书2:', res);
                that.onLoad(wx.getStorageSync('userid'));
              }
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  //删除荣誉证书
  del: function(e) {
    var that = this;
    
     console.log("aaaaa   ", e);
    var id = e.currentTarget.dataset.imgid;
      
    console.log("dddddddddddd   ", id);
    // 美丽大片播放
    wx.request({
      url: app.globalData.baseUrl + '/doctor/doctor/deleteHonor',
      data: {
        honorId: id ,
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success: function(res) {
        // console.log("删除证书成功", res.data);
        that.onLoad(wx.getStorageSync('userid'));
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      
      url: app.globalData.imgUrl
    })
    var that = this;
    
   
    wx.request({
      url: app.globalData.baseUrl + '/doctor/doctor/getHonor',
      data: {
        doctorId: wx.getStorageSync('userid'),
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success: function(res) {
        console.log("证书显示", res.data.data);
       
        that.setData({
          'zhengshu': res.data.data,
          
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})